import Vue from 'vue'
import Vuex from 'vuex'
import { loginAPI } from '@/api'
Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    token: localStorage.getItem('token') || '',
    refreshToken: localStorage.getItem('refreshToken') || ''
  },
  mutations: {
    setToken (state, tokenStr) {
      state.token = tokenStr
      localStorage.setItem('token', tokenStr)// 存到本地
    },
    setRefreshToken (state, refreshTokenStr) {
      state.refreshToken = refreshTokenStr
      localStorage.setItem('refreshToken', refreshTokenStr)
    }
  },
  // *****************************************请求数据异步代码
  actions: {
    async asyncLogin (store, formLogin) {
      const res = await loginAPI(formLogin)
      const { token, refresh_token: refreshToken } = res.data// 结构
      store.commit('setToken', token)
      store.commit('setRefreshToken', refreshToken)
    }
  },
  modules: {
  }
})
